﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Supplier
{
    public class SupplierQueryHandler : IRequestHandler<SupplierQueryCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Suppliers> suppliersRepository;
        private readonly ILogger<SupplierQueryHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="suppliersRepository"></param>
        public SupplierQueryHandler(IBaseRepositoryGet<Suppliers> suppliersRepository, ILogger<SupplierQueryHandler> logger)
        {
            this.suppliersRepository = suppliersRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SupplierQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            try
            {
                string sql = "select *from Suppliers where IsDelete=0";
                
                var list = suppliersRepository.GetAll(sql,default).ToList();

                if (!string.IsNullOrEmpty(request.SuppliersNo))
                {
                    list = list.Where(x => x.SuppliersNo.Contains(request.SuppliersNo)).ToList();
                }

                if (!string.IsNullOrEmpty(request.SuppliersName))
                {
                    list = list.Where(x => x.SuppliersName.Contains(request.SuppliersName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SuppliersType))
                {
                    list = list.Where(x => x.SuppliersType.Contains(request.SuppliersType)).ToList();
                }

                if (request.CountryId > 0)
                {
                    list = list.Where(x => x.CountryId == request.CountryId).ToList();
                }

                //if (request.ProcessRouteStatus != null)
                //{
                //    list = list.Where(x => x.ProcessRouteStatus == request.ProcessRouteStatus).ToList();
                //}
                var totalCount = list.Count();

                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);

                list = list.OrderBy(x => x.SuppliersId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                var aa = new
                {
                    list,
                    totalCount,
                    totalPage
                };
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = aa;
                    result.Msg = "显示成功";
                    logger.LogInformation("供应商管理显示成功");
                }
                else
                {
                    result.Msg = "显示失败";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("供应商管理显示失败"+ex.Message);
                throw;
            }
            return result;
        }



    }
}
